package common;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.support.ui.WebDriverWait;
import tests.LoginPage;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Duration;

public class Utils {
    //创建详情页url
    public static String detailUrl;
    //创建驱动对象
    public static WebDriver driver=null;
    public WebDriverWait wait=null;
    //构造方法,父类直接构造，子类调用父类构造
    public  Utils(String url){
        driver=createDriver();
        driver.get(url);
         wait = new WebDriverWait(driver,Duration.ofSeconds(10));
    }
    //创建驱动
    public static WebDriver createDriver() {
        if(driver==null) {
            //下载驱动
            // WebDriverManager.edgedriver().setup();
            //创建驱动
            System.setProperty("webdriver.edge.driver", "D:\\edgedriver_win64\\msedgedriver.exe");
            EdgeOptions options = new EdgeOptions();
            options.addArguments("--remote-allow-origins=*");
            driver = new EdgeDriver(options);
            driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        }
        return driver;
    }
    //屏幕截图
    //./src/test/java/images/
                     // /2025-01-01/
                     //   /test01-12341456.png
                     //   /test02-12341456.png
                     //   /test03-12341456.png
                      ///2025-01-02/
                      //   /test01-12341456.png
                      //   /test02-12341456.png
                      //   /test03-12341456.png

    public void ScreenShot(String str) throws IOException {
        //年月日
        SimpleDateFormat sim1 = new SimpleDateFormat("yyyy-MM-dd");
        //时分秒
        SimpleDateFormat sim2 = new SimpleDateFormat("HH-mm-ss-SS");
        //获取当前时间
        //时间戳
        String dirTime = sim1.format(System.currentTimeMillis());
        String time = sim2.format(System.currentTimeMillis());

        //完整的路径 ：./src/test/java/images/2025-01-01/test01-12341456.png
        //需要一个方法名，然后定义一个String str 的形参
        String filename= "./src/test/java/images/" + dirTime + "/" + "test" + str + "-" + time + ".png";

        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile, new File(filename));
    }


}
